package com.fpost.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.fpost.constants.ActionConstants;
import com.fpost.domain.Article;
import com.fpost.form.AjaxTestForm;
import com.fpost.form.dto.ArticleDTO;
import com.fpost.manager.ArticleManager;
import com.fpost.util.CommonUtils;

public class AjaxTestAction extends Action {
	
	private static Logger log = Logger.getLogger(AdvancedSearchAction.class);
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		//set current path in session
		CommonUtils.setCurrentPathInSession(request, mapping);
		
		String forward = ActionConstants.SUCCESS;
//		AjaxTestForm ajaxTestForm = (AjaxTestForm)form;
//		
//		Calendar date = GregorianCalendar.getInstance();
//		date.add(Calendar.MONTH, -1);
//		ajaxTestForm.setMostRecentArticles(getMostRecentCurrentArticles(date.getTime(), new Date(), ActionConstants.MAX_RECENT_RESULTS));
		
		return mapping.findForward(forward);
	}
	
	private List<ArticleDTO> getMostRecentCurrentArticles(Date from , Date to, int maxResults) throws Exception {
		
		List<Article> recentArticles = 
				new ArticleManager().getMostRecentCurrentArticles(from , to, maxResults);
		List<ArticleDTO> articleDTOList = new ArrayList<ArticleDTO>();
		for (Article article : recentArticles) {
			ArticleDTO articleDTO = new ArticleDTO();
			articleDTO.setSnapshotProperties(article);
			articleDTOList.add(articleDTO);
		}		
		return articleDTOList;
	}
	
}
